Conditions | 1 |
Paths | 1 |
Total Lines | 223 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
16 | function (state, data, visibility, util, format, reaction) { |
||
17 | let ct = this; |
||
18 | ct.state = state; |
||
19 | ct.data = data; |
||
20 | ct.util = util; |
||
21 | ct.format = format; |
||
22 | ct.reaction = reaction; |
||
23 | |||
24 | function update(player) { |
||
25 | processRedox(player); |
||
26 | processElectronegativity(player); |
||
27 | } |
||
28 | |||
29 | function processRedox(player){ |
||
30 | for(let slot of player.element_slots){ |
||
31 | if(!slot){ |
||
32 | continue; |
||
33 | } |
||
34 | for (let redox of slot.redoxes) { |
||
35 | if (!redox.resource || !redox.active) { |
||
36 | continue; |
||
37 | } |
||
38 | |||
39 | let reactant = ct.generateName(redox.element, redox.from); |
||
40 | let power = ct.redoxPower(player); |
||
41 | let number = Math.min(power, player.resources[reactant].number); |
||
42 | let react = ct.redoxReaction(redox); |
||
43 | |||
44 | ct.reaction.react(number, react, player); |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 | |||
49 | function processElectronegativity(player){ |
||
50 | for(let key in data.elements){ |
||
51 | let element = data.elements[key]; |
||
52 | if(element.electronegativity === 0){ |
||
53 | continue; |
||
54 | } |
||
55 | let ions = element.anions.concat(element.cations); |
||
56 | ions.push(element.main); |
||
57 | for(let resource of ions){ |
||
58 | if(player.resources[resource].number === 0){ |
||
59 | continue; |
||
60 | } |
||
61 | let charge = data.resources[resource].charge || 0; |
||
62 | let probabilities = probabilityDistribution(key, charge); |
||
63 | for(let probKey in probabilities){ |
||
64 | if(probKey === charge){ |
||
65 | continue; |
||
66 | } |
||
67 | let production = Math.floor(probabilities[probKey]*player.resources[resource].number); |
||
68 | if(production === 0){ |
||
69 | continue; |
||
70 | } |
||
71 | let react = ct.redoxReaction({ |
||
72 | element: key, |
||
73 | from: charge, |
||
74 | to: parseInt(probKey, 10) |
||
75 | }); |
||
76 | // electronegativity is 'for free' |
||
77 | react.reactant.eV = 0; |
||
78 | // FIXME: starvation should fix this |
||
79 | if(react.reactant['e-']){ |
||
80 | production = Math.min(production, player.resources['e-'].number); |
||
81 | } |
||
82 | ct.reaction.react(production, react, player); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | function probabilityDistribution(element, charge){ |
||
89 | let prob = {}; |
||
90 | let start = -data.elements[element].electron_affinity.length; |
||
91 | let end = charge; |
||
92 | // lower than index, affected by negativity |
||
93 | rangeProbability(element, prob, start, end, 1, data.elements[element].negative_factor); |
||
94 | |||
95 | prob[charge] = 1; |
||
96 | |||
97 | start = charge+1; |
||
98 | end = data.elements[element].ionization_energy.length+1; |
||
99 | // lower than index, affected by positivity |
||
100 | rangeProbability(element, prob, start, end, -1, data.elements[element].positive_factor); |
||
101 | |||
102 | let sum = 0; |
||
103 | for(let i in prob){ |
||
104 | sum += prob[i]; |
||
105 | } |
||
106 | for(let i in prob){ |
||
107 | prob[i] /= sum; |
||
108 | } |
||
109 | return prob; |
||
110 | } |
||
111 | |||
112 | function rangeProbability(element, prob, start, end, offset, factor){ |
||
113 | for(let i = start; i < end; i++){ |
||
114 | let difference = data.redox[element][i]-data.redox[element][i+offset]; |
||
115 | if(difference <= 0){ |
||
116 | difference = -difference; |
||
117 | }else{ |
||
118 | difference = 1/difference; |
||
119 | } |
||
120 | prob[i] = data.constants.ELECTRONEGATIVITY_CHANCE*factor*difference; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /* Calculates the redox power based on the redox upgrades */ |
||
125 | ct.redoxPower = function(player) { |
||
126 | let level = player.global_upgrades.redox_bandwidth; |
||
127 | let upgrade = data.global_upgrades.redox_bandwidth; |
||
128 | let basePower = upgrade.power; |
||
129 | let polynomial = upgrade.power_poly; |
||
130 | return basePower * Math.floor(Math.pow(level, polynomial)); |
||
131 | }; |
||
132 | |||
133 | /* Writes a redox in the form of a reaction so that we can use the reaction |
||
134 | service to process it */ |
||
135 | ct.redoxReaction = function (redox) { |
||
136 | let reactant = ct.generateName(redox.element, redox.from); |
||
137 | let product = ct.generateName(redox.element, redox.to); |
||
138 | let energy = redoxEnergy(redox.from, redox.to, redox.element); |
||
139 | |||
140 | let react = { |
||
141 | 'reactant': {}, |
||
142 | 'product': {} |
||
143 | }; |
||
144 | |||
145 | react.reactant[reactant] = 1; |
||
146 | react.product[product] = 1; |
||
147 | if (energy > 0) { |
||
148 | react.reactant.eV = energy; |
||
149 | } else if (energy < 0) { |
||
150 | react.product.eV = -energy; |
||
151 | } |
||
152 | |||
153 | let electron = redox.from - redox.to; |
||
154 | if (electron > 0) { |
||
155 | react.reactant['e-'] = electron; |
||
156 | } else if (electron < 0) { |
||
157 | react.product['e-'] = -electron; |
||
158 | } |
||
159 | |||
160 | return react; |
||
161 | }; |
||
162 | |||
163 | /* Calculates how much energy it takes to go from a redox level to another |
||
164 | for a given element */ |
||
165 | function redoxEnergy(from, to, element) { |
||
166 | let energyFrom = data.redox[element][from]; |
||
167 | let energyTo = data.redox[element][to]; |
||
168 | let energy = energyTo - energyFrom; |
||
169 | |||
170 | return energy; |
||
171 | } |
||
172 | |||
173 | /* Generates the name of a ion, e.g. O3+ */ |
||
174 | ct.generateName = function (element, i) { |
||
175 | if (i === 0) { |
||
176 | return data.elements[element].main; |
||
177 | } |
||
178 | let postfix = ''; |
||
179 | if (Math.abs(i) > 1) { |
||
180 | postfix = Math.abs(i); |
||
181 | } |
||
182 | postfix += getSign(i); |
||
183 | let name = element + postfix; |
||
184 | // special case!! H+ is just a proton |
||
185 | if (name === 'H+') { |
||
186 | name = 'p'; |
||
187 | } |
||
188 | return name; |
||
189 | }; |
||
190 | |||
191 | function getSign(number) { |
||
192 | return number > 0 ? '+' : '-'; |
||
193 | } |
||
194 | |||
195 | /* Calculates the number of redox slots based on the redox upgrades */ |
||
196 | ct.redoxSlots = function (player) { |
||
197 | let level = player.global_upgrades.redox_slots; |
||
198 | let upgrade = data.global_upgrades.redox_slots; |
||
199 | let basePower = upgrade.power; |
||
200 | let multiplier = upgrade.power_mult; |
||
201 | return basePower * Math.floor(multiplier * level); |
||
202 | }; |
||
203 | |||
204 | ct.redoxSize = function (player) { |
||
205 | let size = 0; |
||
206 | for(let slot of player.element_slots){ |
||
207 | if(!slot){ |
||
208 | continue; |
||
209 | } |
||
210 | size += slot.redoxes.length; |
||
211 | } |
||
212 | return size; |
||
213 | }; |
||
214 | |||
215 | /* Adds a new redox to the player list */ |
||
216 | ct.addRedox = function (player, slot) { |
||
217 | if(ct.redoxSize(player) >= ct.redoxSlots(player)){ |
||
218 | return; |
||
219 | } |
||
220 | slot.redoxes.push({ |
||
221 | resource: data.elements[slot.element].main, |
||
222 | active: false, |
||
223 | element: slot.element, |
||
224 | from: 0, |
||
225 | to: 1 |
||
226 | }); |
||
227 | }; |
||
228 | |||
229 | ct.removeRedox = function (slot, index) { |
||
230 | slot.redoxes.splice(index, 1); |
||
231 | }; |
||
232 | |||
233 | ct.visibleRedox = function(slot) { |
||
234 | return slot.redoxes; |
||
235 | }; |
||
236 | |||
237 | state.registerUpdate('redox', update); |
||
238 | } |
||
239 | ]); |
||
240 |